#include <iostream>
#include <stdio.h>
#include <errno.h>
#include <sys/timerfd.h>
#include <unistd.h>

int main()
{
    int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
    if(timerfd < 0)
    {
        perror("timerfd_create error");
        exit(2);
    }

    struct itimerspec itm;
    itm.it_value.tv_sec = 3;
    itm.it_value.tv_nsec = 0;
    itm.it_interval.tv_sec = 3;
    itm.it_interval.tv_nsec = 0;

    timerfd_settime(timerfd, 0, &itm, nullptr);

    while(true)
    {
        uint64_t tmp;
        int n = read(timerfd, &tmp, sizeof tmp);
        if(n < 0)
        {
            perror("read error");
            exit(3);
        }
        std::cout << "超时了，距离上一次超时: " << tmp << " 次数" << std::endl; 
    }
    return 0;
}

